<template>
	<view class="page index">
		<view class="form">
			<picker mode="selector" :range="customerList" @change="selectCustomer">
				<view class="picker">
					<common-input
					  title="所属客户" 
						placeholder="请选择" 
						v-model="customerIndex < 0 ? '' : customerList[customerIndex]"
						:border-bottom="false"
						:disabled="true"
					>
					</common-input>
				</view>
			</picker>
			<common-input
			  title="项目名称" 
				placeholder="请填写项目名称" 
				v-model="name"
				:border-bottom="false"
			>
			</common-input>
			<common-input
			  title="项目编号" 
				placeholder="请填写项目编号" 
				v-model="code"
				:border-bottom="false"
			>
			</common-input>
			<picker mode="selector" :value="typeIndex" :range="typeList" @change="selectType">
				<view class="picker">
					<common-input 
					  title="项目类型" 
					  placeholder="请选择项目类型" 
					  v-model="typeIndex < 0 ? '' : typeList[typeIndex]" 
					  :select="true" 
					  :disabled="true" 
						:border-bottom="false"
					>
					</common-input>
				</view>
			</picker>
			<picker mode="selector" :value="levelIndex" :range="levelList" @change="selectLevel">
				<view class="picker">
					<common-input 
					  title="项目等级" 
					  placeholder="请选择项目等级" 
					  v-model="levelIndex < 0 ? '' : levelList[levelIndex]" 
					  :select="true" 
					  :disabled="true" 
						:border-bottom="false"
					>
					</common-input>
				</view>
			</picker>
			<view class="picker" @click="selectContact">
				<common-input
				  title="项目联系人" 
					placeholder="请选择项目联系人" 
					v-model="contact"
					:disabled="true"
				>
				</common-input>
			</view>
			<view class="text">
				<view class="title">
					<view>
						项目描述
					</view>
					<!-- <view class="require"></view> -->
				</view>
				<textarea v-model="explain" placeholder="请输入项目描述" placeholder-class="place"></textarea>
			</view>
			<!-- <common-input
			  title="项目负责人" 
				placeholder="请选择项目负责人" 
				v-model="head"
				:boder-top="false"
				:disabled="true"
			>
			</common-input> -->
			<view class="text">
				<view class="title">
					<view>
						备注
					</view>
					<!-- <view class="require"></view> -->
				</view>
				<textarea v-model="remark" placeholder="请输入备注消息" placeholder-class="place"></textarea>
			</view>
		</view>
		<view class="footer">
			<button @click="save">保存</button>
		</view>
		<common-select :show="showSelect" :list="contactList" @changePopup="changeShow" @select="select"></common-select>
	</view>
</template>

<script>
	import commonInput from "@/components/commonInput.vue";
	import commonSelect from "@/components/commonSelect.vue";
	import {getCustomerListApi,getContactApi} from "@/api/common.js";
	import {createProjectApi} from "@/api/project_api.js";
	export default{
		data() {
			return{
				customerIndex:-1,
				customerList:['还没有可选择客户，请先创建客户'],
				customerId:[],
				name: '',
				code:'',
				typeIndex:0,
				typeList:['产品开发类', '运维服务类', '设备销售类'],
				levelIndex:0,
				levelList:['A（重要）', 'B（关注）', 'C（一般）'],
				contact:'',
				contactId:'',
				contactList:[],//联系人列表
				explain: '',
				remark:'',
				showSelect:false,
				isCustomerId: 0,
			}
		},
		components: {
			commonInput,
			commonSelect
		},
		onLoad(e) {
			if(e.customerId != undefined){
				this.isCustomerId = e.customerId;
			}
		},
		onShow() {
			uni.showLoading({
				mask:true,
				title:'加载中...'
			})
			this.getCustomerList();
		},
		methods:{
			// 获取客户列表
			async getCustomerList(){
				let res = await getCustomerListApi();
				if(this.isCustomerId != 0){
					res.data = res.data.filter((item) => {
						return item.id == this.isCustomerId;
					})
					this.customerIndex = 0;
				}
				this.customerList = res.data.map((item) => item.name);
				this.customerId = res.data.map((item) => item.id);
				if(this.customerIndex == 0){
					this.getContact();
				}
				await uni.hideLoading();
			},
			selectCustomer(e){
				if(this.customerId.length == 0){
					uni.navigateTo({
						url:'/pages/index/customer/createCustomer'
					})
					return;
				}
				if(this.customerIndex == e.detail.value){
					return;
				}
				this.customerIndex = e.detail.value;
				this.getContact();
			},
			// 联系人
			selectContact(){
				if(this.customerIndex == -1){
					uni.showToast({
						icon:'none',
						title:'请先选择客户'
					})
					return;
				}
				this.showSelect = true;
			},
			changeShow(e){
				this.showSelect = e;
			},
			select(e){
				this.contact = e.map((item) => item.name).join(',');
				this.contactId = e.map((item) => item.value);
			},
			// 获得联系人列表
			getContact(){
				this.contact = '';
				getContactApi(this.customerId[this.customerIndex]).then((res) => {
					res.data.list = res.data.list.map((item) => {
						let value = item.id;
						let name = item.name;
						return{
							value,
							name
						}
					})
					this.contactList = res.data.list;
				})
			},
			// 联系人结束
			// 类型
			selectType(e){
				console.log(e)
				this.typeIndex = e.detail.value;
			},
			// 等级
			selectLevel(e){
				this.levelIndex = e.detail.value;
			},
			// 创建项目
			save(){
				if(this.customerIndex < 0){
					uni.showToast({
						icon:'none',
						title:'所属客户不能为空'
					})
					return;
				}
				if(this.name == ''){
					uni.showToast({
						icon:'none',
						title:'项目名称不能为空'
					})
					return;
				}
				if(this.code == ''){
					uni.showToast({
						icon:'none',
						title:'项目编号不能为空'
					})
					return;
				}
				if(this.contact == ''){
					uni.showToast({
						icon:'none',
						title:'项目联系人不能为空'
					})
					return;
				}
				uni.showLoading({
					mask:true,
					title:'加载中...'
				})
				createProjectApi({
					customerId: this.customerId[this.customerIndex],
					name: this.name,
					projectNo: this.code,
					type: this.typeIndex,
					level: this.levelIndex,
					contactIds: this.contactId,
					description: this.explain,
					remark: this.remark
				}).then(() => {
					uni.hideLoading();
					uni.showToast({
						icon: 'success',
						title: '创建成功！'
					})
					setTimeout(() => {
						uni.navigateBack();
					},1500)
				}).catch(() => {
					uni.hideLoading();
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/common/style/customicons.scss";
	.index{
		padding: 24rpx 0;
		@include flex-c;
		.form{
			flex: 1 0 0;
			overflow: auto;
			.text{
				border-bottom: 4rpx #e5e5e5 solid;
				padding: 12rpx 30rpx;
				.title{
					font-size: 28rpx;
					color: #101010;
					padding: 0 0 16rpx;
					display: flex;
					align-items: center;
					.require{
					  height: 28rpx;
					  width: 6rpx;
					  background-color: #c24135;
					  margin-left: 10rpx;
				  }
				}
				textarea{
					font-size: 28rpx;
					color: #909090;
					height: 125rpx;
				}
				.place{
					font-size: 28rpx;
					color: #909090;
				}
			}
			&::-webkit-scrollbar{
				display: none;
			}
		}
		.footer{
			flex: 0 0 auto;
			padding: 24rpx 130rpx;
			button{
				@include button;
				background-color: #198AFA;
				color: #fff;
				height: 60rpx;
				line-height: 60rpx;
				font-size: 28rpx;
				width: 100%;
			}
		}
	}
</style>